package test.adddata.dao4update;

import cn.hutool.core.util.ZipUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.ds.druid.DruidDSFactory;
import lombok.SneakyThrows;
import utils.Utils;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author: gcr
 * @Date: 2022/3/30 19:54
 */
public class 不存在xml部分插入刀片4_2 {
    @SneakyThrows
    public static void main(String[] args) {

        String from = "xml_content_20200320_noexist";
        String to = "xml_content";

        DSFactory.setCurrentDSFactory(new DruidDSFactory());
        DataSource dsServer = DSFactory.get("server_dao4");
        Db useLocal = Db.use();
        Db useServer = Db.use(dsServer);
        List<Entity> query = Utils.getAllDbInfoByPriID("pmid", from, useLocal, 12);

        query.parallelStream().forEach(entity -> {
            String content = ZipUtil.unGzip(entity.getBytes("content"), "utf-8");
            Integer pmid = entity.getInt("pmid");
            try {
                useServer.insert(new Entity().setTableName(to).set("pmid", pmid).set("content", ZipUtil.gzip(content, "UTF-8")));
                Entity where = new Entity();
                where.setTableName(from);
                where.set("pmid", pmid);
                int del = useLocal.del(where);
            } catch (SQLException e) {
                String message = e.getMessage();
                if (message.contains("PRIMARY")) {
                    try {
                        Entity where = new Entity();
                        where.setTableName(from);
                        where.set("pmid", pmid);
                        int del = useLocal.del(where);
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
            }
        });

    }
}
